package adminServlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import userDao.UserDao;
import userEntity.User;
import userImplment.UserImplement;

/**
 * Servlet implementation class AdminAddUser
 */
public class AdminAddUser extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminAddUser() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String Uname = request.getParameter("Uname");
        String passWord = request.getParameter("passWord");
        String idType = request.getParameter("idType");
        String idCard = request.getParameter("idCard");
        String phone = request.getParameter("phone");
        int balance = Integer.parseInt(request.getParameter("balance"));
        //将表单获得的用户注册信息封装为一个User对象
        User user = new User();
        user.setUname(Uname);
        user.setPassWord(passWord);
        user.setIdType(idType);
        user.setIdCard(idCard);
        user.setPhone(phone);
        user.setBalance(balance);
        HttpSession session = request.getSession();
        UserDao userImplement = new UserImplement();

        if (userImplement.AddUser(user)) {
            session.setAttribute("userList", userImplement.GetAllUser());
            request.getRequestDispatcher("adminUser.jsp").forward(request, response);
        } else {
            //插入失败跳转到出错提示页面
            System.out.print("添加用户失败");
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
